// Chat Program for PCMod (SERVER)

PROG.PName = "AChat"
PROG.Author = "[GU]|ZLC|thomasfn"

function PROG:Initialize( pl )
	SendClientProgram( pl, "AChat" )
	SendClientWindow( pl, "Main" )
	
	OpenPort( pl, 101 )
	
	self:PC_Changed( pl )
end

function PROG:PC_Changed( pl )
	// See if the player has actually used the PC
	pc = pl.CurrentPC
	if (!pc) then return end
	
	// Store the PC info
	self.PC = pc
	self.PCID = pc:EntIndex()
	
	// Init some vars on the PC
	if (!PData[self.PCID]) then PData[self.PCID] = {} end
	if (!PData[self.PCID].LastIP) then PData[self.PCID].LastIP = "192.168.0.1" end
	
	// Beam some data to new player
	self:BeamLastIP( pl )
	BeamData_String( ply, "update", "" )
	
	// Store the player for later use
	PData[self.PCID].LPlayer = pl
end

function PROG:Unload()
	if (PData[self.PCID].LPlayer) then ClosePort( PData[self.PCID].LPlayer, 101 ) end
end

function PROG:Think()

end

function PROG:CommandRecieved( ply, command, args )
	local pc = ply.CurrentPC
	if (command == "sendchat") then
		// Get the target IP, and remove it from args
		local target = args[1]
		table.remove( args, 1 )
		
		// Convert the args table into text
		local text = table.concat( args, " " )
		
		// Send the chat message
		local result = ComposePacket( ply, target, 101, { "chat", text } )
		
		// If successful, beam the text back to client
		// If not, tell the client why
		if (result == 1) then
			local chattext = "[b]To " .. target .. ": " .. text
			self:AddChatMessage( pc, ply, chattext )
		else
			self:AddChatMessage( pc, ply, "Cannot deliver - " .. DeliveryStatus( result ) )
		end
		
		// Set the last IP
		PData[self.PCID].LastIP = target
		
		// Tell client to update their chat area
		if (ply) then BeamData_String( ply, "update", "" ) end
	end
	if (command == "clear") then
		pc:SetNWInt( "achat_msgs", 0 )
	end
	if (command == "quit") then
		UnloadProgram( ply )
	end
end

function PROG:PacketRecieved( packet, pc )
	pcid = pc:EntIndex()
	if (packet[4][1] == "chat") then
		// We have a chat message, build the string
		chattext = packet[1] .. ": " .. packet[4][2]
		
		// Add the chat message
		self:AddChatMessage( pc, PData[self.PCID].LPlayer, chattext )
		
		// Tell player to update their client
		if (ply) then BeamData_String( ply, "update", "" ) end
	end
end

function PROG:AddChatMessage( pc, ply, message )
	// Get some message details
	msgcnt = pc:GetNWInt( "achat_msgs" )
	msgid = msgcnt+1
	
	// Add the message to the PC
	pc:SetNWInt( "achat_msgs", msgid )
	
	// We are limited to 40 chars per NW String
	// We are only building messages out of 2 strings = 80 chars, - 17 for IP = 63 characters
	if (string.len( message ) < 41 ) then
		pc:SetNWString( "achat_msg_" .. tostring(msgid) .. "_1", message )
	else
		pc:SetNWString( "achat_msg_" .. tostring(msgid) .. "_1", string.Left(message,40) )
		if (string.len( message ) < 81 ) then
			pc:SetNWString( "achat_msg_" .. tostring(msgid) .. "_2", string.Right( message, string.len( message )-40 ) )
		end
	end
end

function PROG:BeamLastIP( ply )
	BeamData_String( ply, "lastip", PData[self.PCID].LastIP )
end